/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/

using UnityEngine;

/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class CloudRecoTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{
    #region PRIVATE_MEMBER_VARIABLES
 
    private TrackableBehaviour mTrackableBehaviour;
	private CloudRecoEventHandler cloudRecoEventHandler;
	public Transform CloudRecognition;
	private bool flagScreen=false;
   		
     void Awake()
    {
       this.iniciarEscaner();
    }
    #endregion // PRIVATE_MEMBER_VARIABLES



    #region UNTIY_MONOBEHAVIOUR_METHODS
    
    void Start()
    {
		
		cloudRecoEventHandler=CloudRecognition.GetComponent<CloudRecoEventHandler>();
		
        mTrackableBehaviour = GetComponent<TrackableBehaviour>();
        if (mTrackableBehaviour)
        {
            mTrackableBehaviour.RegisterTrackableEventHandler(this);
        }
    }

    #endregion // UNTIY_MONOBEHAVIOUR_METHODS



    #region PUBLIC_METHODS

    /// <summary>
    /// Implementation of the ITrackableEventHandler function called when the
    /// tracking state changes.
    /// </summary>
    public void OnTrackableStateChanged(
                                    TrackableBehaviour.Status previousStatus,
                                    TrackableBehaviour.Status newStatus)
    {
        if (newStatus == TrackableBehaviour.Status.DETECTED ||
            newStatus == TrackableBehaviour.Status.TRACKED ||
            newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
        {
            OnTrackingFound();
		
        }
        else
        {
            OnTrackingLost();
        }
    }
	public bool getFlagScreen(){
		return flagScreen;
	}
	public void setFlagScren(bool flagVar){
		this.flagScreen=flagVar;
	}
    #endregion // PUBLIC_METHODS



    #region PRIVATE_METHODS


    private void OnTrackingFound()
    {

		flagScreen=true;
        // Stop finder since we have now a result, finder will be restarted again when we lose track of the result
        ImageTracker imageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
        if(imageTracker != null)
		{
            imageTracker.TargetFinder.Stop();
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
    }


    private void OnTrackingLost()
    {
		  
		cloudRecoEventHandler.limpiarLista();
        // Start finder again if we lost the current trackable
        ImageTracker imageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
        if(imageTracker != null)
		{
			imageTracker.TargetFinder.ClearTrackables(false);
            imageTracker.TargetFinder.StartRecognition();
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }
	public void iniciarEscaner(){
		ImageTracker imageTracker = TrackerManager.Instance.GetTracker<ImageTracker>();
        if(imageTracker != null)
		{
			imageTracker.TargetFinder.ClearTrackables(false);
            imageTracker.TargetFinder.StartRecognition();
        }
	}
    #endregion // PRIVATE_METHODS
	
}
